﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CEMS.Controls.Commons;
using System.Windows.Forms;

namespace QuanLyNganHangDe.BLL
{
    public class DataGridViewExGroup : DataGridViewEx
    {
        public string ColumnIndexToGroup = "-1";
        int rowIndexNotLoop = -1;
        protected override void OnCellFormatting(
           DataGridViewCellFormattingEventArgs args)
        {
            base.OnCellFormatting(args);
            if (this.DesignMode)
                return;
            if (args.RowIndex == 0)
                //||ColumnIndexToGroup.Contains(args.ColumnIndex.ToString()))
                return;
            if (
                //rowIndexNotLoop != args.RowIndex)
                 IsRepeatedCellValue(args.RowIndex, args.ColumnIndex))
            {
                args.Value = string.Empty;
                args.FormattingApplied = true;
            }
            //else
            //    rowIndexNotLoop = args.RowIndex;
        }

        private bool IsRepeatedCellValue(int rowIndex, int colIndex)
        {
            if (this.DesignMode)
                return false;
            if (!ColumnIndexToGroup.Contains(colIndex.ToString()))
                return false;
            DataGridViewCell currCell =
               Rows[rowIndex].Cells[colIndex];
            DataGridViewCell prevCell =
               Rows[rowIndex - 1].Cells[colIndex];

            if ((currCell.Value == prevCell.Value) ||
               (currCell.Value != null && prevCell.Value != null &&
               currCell.Value.ToString() == prevCell.Value.ToString()))
                return true;
            else
                return false;
        }
    }
}
